<template>
  <div class="side-menu-wrapper">
    <!-- 768px 下，以抽屉形式展示 -->
    <el-drawer :visible.sync="isDrawer" :with-header="false" :size="210"
      direction="ltr" v-if="screenWidth <= 768">
      <!-- collapse 属性：控制菜单收缩展开 -->
      <el-menu class="side-menu" :default-active="activeIndex1" :router="true"
        @select="menuItemClick" :collapse="isCollapse">
        <el-menu-item index="0" :route="{ name: 'Root' }">
          <i class="el-icon-menu"></i>
          <span slot="title">用户管理</span>
        </el-menu-item>
        <el-menu-item index="file" :route="{ name: 'Root' }">
          <i class="el-icon-files"></i>
          <span slot="title">网盘管理</span>
        </el-menu-item>

      </el-menu>
    </el-drawer>
    <!-- 768px 以上，平铺展示 -->
    <template v-else>
      <!-- collapse 属性：控制菜单收缩展开 -->
      <el-menu class="side-menu" :router="true" :default-active="activeIndex1"
        @select="menuItemClick" :collapse="isCollapse">
        <el-menu-item index="0" :route="{ name: 'Root' }">
          <i class="el-icon-menu"></i>
          <span slot="title">用户管理</span>
        </el-menu-item>
        <!-- \:route="{ name: 'File'}" -->
        <el-menu-item index="file" class="recovery">
          <i class="el-icon-files"></i>
          <span slot="title">网盘管理</span>
        </el-menu-item>

      </el-menu>

    </template>
    <!-- 展开 & 收缩分类栏 -->
    <el-tooltip effect="dark" :content="isCollapse ? '展开' : '收起'"
      placement="right">
      <div class="aside-title"
        @click="isCollapse ? (isCollapse = false) : (isCollapse = true)">
        <i class="icon"
          :class="isCollapse ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"
          :title="isCollapse ? '展开' : '收起'"></i>
      </div>
    </el-tooltip>
  </div>
</template>

<script>
export default {
  name: 'SideMenu',
  data() {
    return {
      isDrawer: false, //  控制移动端菜单抽屉是否显示
      isCollapse: false, //  控制菜单收缩展开
      // 菜单 index 和名称 Map
      title: '管理员'

    }
  },
  computed: {
    activeIndex1() {
      return this.$route.name  //  获取当前路由名称|| 'Home'
    },
    // 屏幕宽度
    screenWidth() {
      return this.$store.state.common.screenWidth
    }
  },
  watch: {
    // 监听左侧菜单切换，修改浏览器标签标题
    activeIndex() {
      document.title = `${this.title} - ${this.$config.siteName}`
      this.isDrawer = false
    },
    // 监听收缩状态变化，存储在 localStorage 中，保证页面刷新时仍然保存设置的状态
    isCollapse(newValue) {

      if (this.screenWidth <= 768 && newValue) {
        this.isDrawer = true
        this.isCollapse = false
      }
    }
  },
  methods: {
    menuItemClick(key) {
      if (key === 'File') {
        this.$router.push({ name: key })
      }

    }
  },

  //
  mounted() {
    document.title = `${this.myFileMenuMap[Number(this.activeIndex)]} - ${this.$config.siteName
      }`
  }
}
</script>

<style lang="stylus" scoped>
@import '~_a/styles/varibles.styl';
@import '~_a/styles/mixins.styl';

.side-menu-wrapper {
  position: relative;
  height: calc(100vh - 61px);
  padding-right: 11px;
  .side-menu {
    // 高度设置为屏幕高度减去顶部导航栏的高度
    height: calc(100vh - 127px);
    overflow: auto;
    // 调整滚动条样式
    setScrollbar(6px, transparent, #C0C4CC);
    .el-menu-item.is-active {
      background: #ecf5ff;
    }
    .my-file, .recovery {
      box-shadow: 0 4px 12px 0 $BorderExtralight;
    }
  }
  >>> .el-menu {
    background: transparent;
  }
  // 对展开状态下的菜单设置宽度
  .side-menu:not(.el-menu--collapse) {
    width: 210px;
  }
  // 存储空间展示区
  .storage-wrapper {
    position: absolute;
    bottom: 0;
    left: 0;
    box-shadow: 0 -2px 12px 0 $BorderExtralight;
    border-right: solid 1px #e6e6e6;
    box-sizing: border-box;
    width: calc(100% - 11px);
    height: 66px;
    padding: 16px;
    z-index: 2;
    color: $PrimaryText;
    .text {
      margin-top: 8px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      flex-wrap: wrap;
    }
  }
  .storage-wrapper.fold {
    padding: 0;
    >>> .el-progress--circle {
      margin: 0 auto;
      width: 32px;
      display: block;
    }
    .text {
      font-size: 12px;
      justify-content: center;
    }
  }
  // 折叠图标调整样式
  .aside-title {
    position: absolute;
    top: calc(50% - 50px);
    right: 0;
    z-index: 2;
    background: $BorderBase;
    color: #fff;
    width: 12px;
    height: 100px;
    line-height: 100px;
    cursor: pointer;
    border-radius: 0 16px 16px 0;
    &:hover {
      opacity: 0.7;
    }
    .icon {
      font-size: 12px;
    }
  }
}
</style>
